package com.codeblue.tools.codeGenerator;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

/**
 * 模块  描述业务所属模块
 * @author jiulong.hu 上午10:28:09
 *
 */
public class Module {
	private Project project; //所属项目
	private String moduleName; //模块名 (业务文件夹名称)
	private String ftl_templateDir;
	
	private List<EntityDesc> entityClass= new ArrayList<EntityDesc>();
	

	public List<EntityDesc> getEntityClass() {
		return entityClass;
	}
	
	public boolean isFlag() {
		return flag;
	}
	public void setFlag(boolean flag) {
		this.flag = flag;
	}


	private boolean flag;
	
	/**
	 * 获取controller路径
	 * @return
	 */
	public String getControllerPath() {
		return project.getSrc_diskPath()+ "\\"+ moduleName + "\\controller";
	}
	
	/**
	 * 获取Service路径
	 * @return
	 */
	public String getServicePath() {
		return project.getSrc_diskPath()+ "\\"+ moduleName + "\\service";
	}
	/**
	 * 获取ServiceImpl路径
	 * @return
	 */
	public String getServiceImplPath() {
		return project.getSrc_diskPath()+ "\\"+ moduleName + "\\service" +"\\impl";
	}
	
	/**
	 * 获取vo路径
	 * @return
	 */
	public String getVOPath() {
		return project.getSrc_diskPath()+ "\\"+ moduleName + "\\vo";
	}
	
	/**
	 * 获取mapper路径
	 * @return
	 */
	public String getMapperPath() {
		return project.getSrc_diskPath()+ "\\"+ moduleName + "\\mapper";
	}
	
	/**
	 * 获取model路径
	 * @return
	 */
	public String getModelPath() {
		return project.getSrc_diskPath()+ "\\"+ moduleName + "\\model";
	}
	
	public String getJsPath() {
		return project.getJs_diskPath() ;
	}
	public String getJspPath() {
		return project.getJsp_disPath() ;
	}
	
	
	
	/**
	 * 获取首字母大写的 模块名
	 * @return
	 */
	public String getUpperCaseName(){
		String s1 = this.getModuleName().substring(0, 1).toUpperCase();
		String s2 = this.getModuleName().substring(1, this.getModuleName().length());
		return s1+s2;
	}
	
	
	public Project getProject() {
		return project;
	}
	public void setProject(Project project) {
		this.project = project;
	}
	public String getModuleName() {
		return moduleName;
	}
	public void setModuleName(String moduleName) {
		this.moduleName = moduleName;
	}
	public String getFtl_templateDir() {
		return ftl_templateDir;
	}
	public void setFtl_templateDir(String ftl_templateDir) {
		this.ftl_templateDir = ftl_templateDir;
	}
	
	
	
	/**
	 * 创建业务代码所需文件夹
	 */
	public void createModuleDir(){
		if(StringUtils.isEmpty(moduleName)){
			throw new RuntimeException("模块名不能为空");
		}
		File srcDir = new File(project.getSrc_diskPath()+ "\\"+ moduleName);
		srcDir.mkdir();
		File controllerDir = new File(project.getSrc_diskPath()+ "\\"+ moduleName+"\\"+"view");
		controllerDir.mkdir();
		File serviceDir = new File(project.getSrc_diskPath()+ "\\"+ moduleName+"\\"+"service");
		serviceDir.mkdir();
		File serviceImplDir = new File(project.getSrc_diskPath()+ "\\"+ moduleName+"\\"+"service"+"\\impl");
		serviceImplDir.mkdir();
		File dtoDir = new File(project.getSrc_diskPath()+ "\\"+ moduleName+"\\"+"dto");
		dtoDir.mkdir();
		
		File jspDir = new File(project.getJsp_disPath()+ "\\"+ moduleName);
		jspDir.mkdir();
		
		File jsDir = new File(project.getJs_diskPath()+ "\\"+ moduleName);
		jsDir.mkdir();
		System.out.println("-------------模块" + moduleName +"文件夹创建成功--------------");
	}
	
	/**
	 * 删除整个模块
	 */
	public void destroyAll(){
		deleteDirectory(this.getJsPath()); 
		deleteDirectory(this.getJspPath()); 
		deleteDirectory(this.getProject().getSrc_diskPath()+"\\"+ moduleName); 
	}
	
	
	
    /**
     * 删除目录（文件夹）以及目录下的文件
     * @param   sPath 被删除目录的文件路径
     * @return  目录删除成功返回true，否则返回false
     */
    public boolean deleteDirectory(String sPath) {
        //如果sPath不以文件分隔符结尾，自动添加文件分隔符
        if (!sPath.endsWith(File.separator)) {
            sPath = sPath + File.separator;
        }
        File dirFile = new File(sPath);
        //如果dir对应的文件不存在，或者不是一个目录，则退出
        if (!dirFile.exists() || !dirFile.isDirectory()) {
            return false;
        }
        flag = true;
        //删除文件夹下的所有文件(包括子目录)
        File[] files = dirFile.listFiles();
        for (int i = 0; i < files.length; i++) {
            //删除子文件
            if (files[i].isFile()) {
                flag = deleteFile(files[i].getAbsolutePath());
                if (!flag) break;
            } //删除子目录
            else {
                flag = deleteDirectory(files[i].getAbsolutePath());
                if (!flag) break;
            }
        }
        if (!flag) return false;
        //删除当前目录
        if (dirFile.delete()) {
            return true;
        } else {
            return false;
        }
    }
    
    
    /**
     * 删除单个文件
     * @param   sPath    被删除文件的文件名
     * @return 单个文件删除成功返回true，否则返回false
     */
    public boolean deleteFile(String sPath) {
        flag = false;
        File file = new File(sPath);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            file.delete();
            flag = true;
        }
        return flag;
    }
	
}
